www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/modules/admin/controllers/CategoryController.php

    <?php
class CategoryController extends CController
{
	public function actionValidList()
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'isvalid = 1';
		$criteria->order = 'order_id asc, id asc';
		$categorys = Category::model()->findAll($criteria);
		$this->render('list', array('categorys'=>$categorys));
	}
	
    public function actionInValidList()
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'isvalid = 0';
		$criteria->order = 'order_id asc, id asc';
		$categorys = Category::model()->findAll($criteria);
		$this->render('list', array('categorys'=>$categorys));
	}
	
	public function actionUpdate()
	{
	    if (app()->request->isPostRequest) {
	        foreach ($_POST['name'] as $cid => $v) {
	            $attributes = array(
	                'order_id' => $_POST['order_id'][$cid],
	                'name' => $_POST['name'][$cid],
	            );
	            $result = Category::model()->updateByPk($cid, $attributes);
	            if ($result === false) {
	                throw new CHttpException(400, $v . ' 更新错误...');
	            }
	        }
	        $this->redirect(app()->request->urlReferrer);
	    }
	}
	
	public function actionDelete()
	{
	    if (app()->request->isAjaxRequest && app()->request->isPostRequest) {
			$cid = isset($_GET['cid']) ? $_GET['cid'] : null;
			if (!empty($cid) && is_numeric($cid)) {
    			$result = Category::model()->deleteByPk($cid);
    			$data['result'] = $result ? 1 : 0;
    			$data['message'] = '删除' . ($result ? '成功' : '失败');
			} else {
			    $data['result'] = -1;
    			$data['message'] = '参数错误';
			}
		}
		else {
			$data['result'] = -1;
    		$data['message'] = '非法请求';
		}
		echo json_encode($data);
	}
	
	public function actionMerge()
	{
	    if (app()->request->isPostRequest) {
	        $old = $_POST['category_old'];
	        $new = $_POST['category_new'];
	        $attributes = array('category_id' => $new);
	        $result = Post::model()->updateAll($attributes, "category_id = $old");
	        if ($result !== false) {
	            $category = Category::model();
	            $category->updateByPk($old, array('isvalid'=> Category::NO, 'isshow' => Category::NO));
	            $oldPost = $category->findByPk($old);
	            $counters = array('post_nums' => $oldPost->post_nums);
	            $category->updateCounters($counters, 'id = :new_id', array('new_id' => $new));
	            $category->updateByPk($old, array('post_nums' => 0));
	            unset($category, $oldPost, $counters);
	        }
	    }
		$data = Category::model()->findAll(array(
			'condition' => 'isvalid = 1',
			'order'=>'order_id asc, id asc',
		));
		foreach ($data as $c) {
		    $categorys[$c->id] = $c->name;
		}
		
		$this->render('merge', array('categorys' => $categorys, 'result'=>$result));
	}

	public function actionCreate()
	{
	    if (app()->request->isPostRequest) {
	        $name = trim($_POST['name']);
	        
	        if (empty($name)) {
	            $prompt['result'] = false;
	            $prompt['note'][] = array('text' => '分类名称不能为空');
	            $prompt['note'][] = array('text' => '重新添加分类', 'url'=>url('admin/category/create'));
	            $prompt['note'][] = array('text' => '返回重试', 'url'=>'javascript:history.back();');
	            $this->render('create', array('prompt'=>$prompt));
	            exit(0);
	        }
	        
	        $c = Category::model()->findByAttributes(array('name' => $name));
	        if ($c !== null) {
	            $prompt['result'] = false;
                $prompt['note'][] = array('text' => $name . '&nbsp;分类已经存在');
	            $prompt['note'][] = array('text' => '添加新的分类', 'url'=>url('admin/category/create'));
	        } else {
    	        $category = new Category();
    	        $category->name = $name;
    	        $category->remark = $_POST['remark'];
    	        $category->order_id = $_POST['order_id'];
    	        $category->isvalid = $_POST['isvalid'];
    	        $category->isshow = $_POST['isshow'];
    	        $result = $category->save();
    	        
    	        if ($result) {
    	            $prompt['result'] = true;
                    $prompt['note'][] = array('text' => $name . '&nbsp;分类添加成功');
	                $prompt['note'][] = array('text' => '继续添加分类', 'url'=>url('admin/category/create'));
	                $prompt['note'][] = array('text' => '查看主题列表', 'url'=>url('admin/category/list'));
    	        } else {
    	            $prompt['result'] = false;
                    $prompt['note'][] = array('text' => $name . '&nbsp;分类添加失败');
	                $prompt['note'][] = array('text' => '重新添加分类', 'url'=>url('admin/category/create'));
	                $prompt['note'][] = array('text' => '返回重试', 'url'=>'javascript:history.back();');
    	        }
	        }
	    }
	    $this->render('create', array('prompt'=>$prompt));
	}
}